import { _decorator, Component, find, instantiate, Label, Node, Sprite, SpriteFrame, } from 'cc';
import { WsMgr } from '../TCP/WsMgr';
import { AbsAdapter, ListView } from '../Common/ListView';
import { MatchRankingAdapter } from './MatchRankingAdapter';
import { Utils } from '../Common/Utils';
import { RankingResult } from '../Cmd/RankingResult';

const { ccclass, property } = _decorator;

@ccclass('MatchRanking')
export class MatchRanking extends Component {

    @property(Node)
    private GameList:Node;
   
    @property(ListView)
    private scoreRankListView:ListView;

    @property(Label)
    private Ranking!:Label;

    @property(Label)
    private Name!:Label;

    @property(Label)
    private Point!:Label;

    private _scoreRankListAdapter: MatchRankingAdapter | null = null;
  
    getscoreRankListAdapter(): MatchRankingAdapter {
    if (!this._scoreRankListAdapter) {
        this._scoreRankListAdapter = new MatchRankingAdapter();
      }
      return this._scoreRankListAdapter;
    }    

   

    start() {
        console.log('MatchRanking start');
       
    }

    onLoad() { 
    }

    updateGameList(ranking :RankingResult[]){
        
        this.getscoreRankListAdapter().setDataSet(ranking);
        console.log('MatchRanking=',ranking);
        this.scoreRankListView.setAdapter(this._scoreRankListAdapter);
        //更新自己
        for (let index = 0; index < ranking.length; index++) {
            const element = ranking[index];
            if (WsMgr.getInstance().getLoginInfo().uid==element.uid) {
               this. onUpdateSelf(element);
               break;
            }
            
        }
        
    }

    onClose(){
        this.GameList.active=false;
    }
    
    onUpdateSelf(itemData:RankingResult ){
       
        this.Ranking.string =itemData.rankingId+'';
        this.Name.string =itemData.name +'';
        this.Point.string = Utils.num1000(itemData.scorePoints) ; 
    }
   

    
}


